package tree;

import tree.define.BinaryTree;
import tree.define.TreeNode;

import java.util.ArrayList;
import java.util.List;

public class 层序遍历 {
    List<List<Integer>> res = new ArrayList<>();

    public static void main(String[] args) {
        BinaryTree mytree=new BinaryTree();
    }

    public List<List<Integer>> levelOrder(TreeNode root) {
        if (root == null)
            return res;
        List<Integer> a = new ArrayList<>();
        a.add(root.val);
        res.add(a);
        helper(root);
        return res;
    }

    public void helper(TreeNode root) {
        //边界条件
        if (root.left == null && root.right == null)
            return;
        List<Integer> list = new ArrayList<>();
        if (root.left != null) {
            list.add(root.left.val);
            if (root.right != null)
                list.add(root.right.val);
            res.add(list);
            helper(root.left);
            helper(root.right);
        }
    }

}
